package actions;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.RowSet;

import model.ReviewerModel;
import model.StudentModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import db.DbException;

public class ReferAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws DbException {
        // username of the reccomended reviewer
        String username = "";
        // id for the reccomended reviewer
        int reviewerid = 0;
        // id for the student to be graded
        int studentid = 0;
        
        // get reccomended reviewer from request scope
        username = request.getParameter("reccomend");
        reviewerid = ReviewerModel.fetchIdByUsername(username);
        studentid = (Integer) request.getSession(false).getAttribute("studentid");
        
        // only enter code if this reviewerid pid combination doesn't exist
        if(!ReviewerModel.hasReview(reviewerid, studentid)){
            // INESRT INTO WORKFLOW A REVIEW FOR THAT REVIEWER
            ReviewerModel.insertReview(studentid, reviewerid);
            // increments number of not reviewed
            ReviewerModel.incrementNotReviewed(reviewerid, 1);
        }
                        
        return mapping.findForward("success");
    }
}
